package com.example.demojwtsmall.controller;

import com.example.demojwtsmall.entity.User;
import com.example.demojwtsmall.utils.JwtUtils;
import com.example.demojwtsmall.utils.Result;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        String token = JwtUtils.generateToken(user.getUsername());
        return Result.ok().data("token", token);
    }

    @GetMapping("/info") // "token : xxx"
    public Result info(String token) {
        String username = JwtUtils.getClaimsByToken(token).getSubject();
        String url = "https://img1.baidu.com/it/u=1324165140,1816385015&fm=253&fmt=auto&app=120&f=JPEG?w=801&h=500";
        return Result.ok().data("name", username).data("avatar", url);
    }

    @PostMapping("/logout")
    public Result logout() {return Result.ok();}


}
